import json
import time
from selenium import webdriver

class DouyuSpider():
    def __init__(self):
        self.url = 'https://www.douyu.com/directory/all'
        self.driver = webdriver.Chrome()

    # 提取数据
    def get_content_list(self):
        li_list = self.driver.find_elements_by_xpath('//ul[@class="layout-Cover-list"]/li')
        content_list = []
        for li in li_list:
            item = {}
            item['房间名'] = li.find_element_by_xpath('.//div[@class="DyListCover-info"]//h3[@class="DyListCover-intro"]').text
            item['主播名'] = li.find_element_by_xpath('.//h2').text
            item['主播类型'] = li.find_element_by_xpath('.//span[@class="DyListCover-zone"]').text
            item['观众'] = li.find_element_by_xpath('.//span[@class="DyListCover-hot"]').text
            content_list.append(item)
            print(item)
        return content_list

    # 保存数据
    def save_content_list(self, content_list):
        with open('斗鱼.txt', 'a', encoding='utf-8') as f:
            f.write(json.dumps(content_list, ensure_ascii=False, indent=2))

    # 跳转下一页
    def get_next_url(self):
        # 获取输入窗口元素
        for num in range(2, 194):
            input = self.driver.find_element_by_xpath('//span[@class="ListFooter-btn-wrap"]/input')
            input.clear()

            # 向input标签中添加元素（num）
            input.send_keys(num)
            # 点击 Go
            self.driver.find_element_by_xpath('//span[@class="ListFooter-btn-wrap"]/span[@class="ListFooter-btn-label"]').click()
            print('##############')
            time.sleep(5)

            # 调用提取数据函数
            content_list = self.get_content_list()
            # 调用保存数据函数
            self.save_content_list(content_list)

    # 主要逻辑
    def run(self):
        # 发送请求获取响应
        self.driver.get(self.url)
        # 使窗口最大化
        self.driver.maximize_window()
        # 提取数据
        content_list = self.get_content_list()
        # 保存数据
        self.save_content_list(content_list)
        # 获取下一页元素
        self.get_next_url()

if __name__ == '__main__':
    douyu = DouyuSpider()
    douyu.run()

















